package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.OrganizationMember;
import com.example.service.OrganizationMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "组织成员管理接口")
@RestController
@RequestMapping("/api/organization-members")
@RequiredArgsConstructor
public class OrganizationMemberController {

    private final OrganizationMemberService organizationMemberService;

    @Operation(summary = "申请加入组织")
    @PostMapping("/join")
    public Result<Boolean> join(@RequestBody OrganizationMember member) {
        return Result.success(organizationMemberService.joinOrganization(member));
    }

    @Operation(summary = "审核成员申请")
    @PostMapping("/{id}/review")
    public Result<Boolean> review(
            @Parameter(description = "申请ID") @PathVariable Long id,
            @Parameter(description = "状态：1-通过 2-驳回") @RequestParam Integer status,
            @Parameter(description = "驳回原因") @RequestParam(required = false) String rejectReason,
            @Parameter(description = "审核人ID") @RequestParam Long reviewerId) {
        return Result.success(organizationMemberService.reviewMember(id, status, rejectReason, reviewerId));
    }

    @Operation(summary = "更新成员信息")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody OrganizationMember member) {
        return Result.success(organizationMemberService.updateMember(member));
    }

    @Operation(summary = "更新成员角色")
    @PostMapping("/{id}/role")
    public Result<Boolean> updateRole(
            @Parameter(description = "成员ID") @PathVariable Long id,
            @Parameter(description = "角色：0-普通成员 1-管理员 2-组织者") @RequestParam Integer role) {
        return Result.success(organizationMemberService.updateMemberRole(id, role));
    }

    @Operation(summary = "启用/禁用成员")
    @PostMapping("/{id}/toggle")
    public Result<Boolean> toggleStatus(
            @Parameter(description = "成员ID") @PathVariable Long id,
            @Parameter(description = "是否启用") @RequestParam boolean enable) {
        return Result.success(organizationMemberService.toggleMemberStatus(id, enable));
    }

    @Operation(summary = "移除成员")
    @DeleteMapping("/{id}")
    public Result<Boolean> remove(@Parameter(description = "成员ID") @PathVariable Long id) {
        return Result.success(organizationMemberService.removeMember(id));
    }

    @Operation(summary = "批量移除成员")
    @DeleteMapping("/batch")
    public Result<Boolean> batchRemove(@Parameter(description = "成员ID数组") @RequestBody Long[] ids) {
        return Result.success(organizationMemberService.batchRemoveMembers(ids));
    }

    @Operation(summary = "分页查询成员列表")
    @GetMapping("/list")
    public Result<Page<OrganizationMember>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "组织ID") @RequestParam(required = false) Long organizationId,
            @Parameter(description = "志愿者ID") @RequestParam(required = false) Long volunteerId,
            @Parameter(description = "角色") @RequestParam(required = false) Integer role,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        return Result.success(organizationMemberService.getMemberList(pageNum, pageSize, organizationId, volunteerId, role, status));
    }

    @Operation(summary = "获取成员详情")
    @GetMapping("/{id}")
    public Result<OrganizationMember> getInfo(@Parameter(description = "成员ID") @PathVariable Long id) {
        return Result.success(organizationMemberService.getMemberInfo(id));
    }

    @Operation(summary = "检查是否为组织成员")
    @GetMapping("/check-member")
    public Result<Boolean> checkMember(
            @Parameter(description = "组织ID") @RequestParam Long organizationId,
            @Parameter(description = "志愿者ID") @RequestParam Long volunteerId) {
        return Result.success(organizationMemberService.isMember(organizationId, volunteerId));
    }

    @Operation(summary = "检查是否为组织管理员")
    @GetMapping("/check-admin")
    public Result<Boolean> checkAdmin(
            @Parameter(description = "组织ID") @RequestParam Long organizationId,
            @Parameter(description = "志愿者ID") @RequestParam Long volunteerId) {
        return Result.success(organizationMemberService.isAdmin(organizationId, volunteerId));
    }
} 